using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using FirebirdSql.Data.FirebirdClient;

namespace ss_sdk
{
    public static class DataAcessLayer
    {
        public static IDbConnection Conexao()
        {
            FbConnection cn = new FbConnection("User=SYSDBA;Password=masterkey;Database=ssteste;DataSource=localhost;Port=3050;Charset=ISO8859_1;Dialect=3;Role=;Connection lifetime=15;Pooling=true;MinPoolSize=0;MaxPoolSize=50;Packet Size=8192;ServerType=0;");
            return cn;
        }

        public static IDbCommand Comando(string sql, List<Parameter> Parametros, IDbConnection cn, IDbTransaction tr)
        {
            FbConnection Conexao = (FbConnection)cn;
            FbTransaction Transacao = (FbTransaction)tr;
            FbCommand Comando = new FbCommand(sql);
            Comando.Connection = Conexao;
            if (Transacao != null)
                Comando.Transaction = Transacao;
            foreach (Parameter Par in Parametros)
            {
                Comando.Parameters.AddWithValue(Par.Name, Par.Value);
                //Gambiarra pra gravar NULL no banco quando eh uma string vazia
                if (Par.Value is string)
                {
                    if ((string)Par.Value == string.Empty)
                        Comando.Parameters[Par.Name].Value = DBNull.Value;
                }
            }
            return Comando;
        }
    }
}
